

#include "TextElement.h"

#include "Color.h"

#include "TouchController.h"

#include "AnimatedSprite.h"

#include "TexturedPolygon.h"

#include "Callback.h"

#include "ToggleButton.h"

#include "Logger.h"

#include "DemoScene.h"

#include "GameController.h"

#include "TextureCache.h"

#include "BounceTween.h"

#include "MainMenu.h"

#include "Tween.h"

#include "Configuration.h"

#include "Datatypes.h"

#include "FileUtils.h"

#include <iostream>

#include <set>
  
using namespace Meteor;

// -----------------------------------------------------------------------------

DemoScene::DemoScene()
{
    try
    {
    
    //Meteor::TouchController::Instance().AddTouchListener( this);

    mBackground = new ParallaxContainer( "sky.png");

    mNess = new Button( "ness.png", "ness_alt.png", 1);
    
    mNess->SetPosition( Vector2D( 400, 200));
    
    mNess->SetSwallowsTouches( true);
    
    //Sprite* cloud = new Sprite( "cloud.png", 4);
    
    //cloud->SetPosition( 400, 450);
    
    //mBackground->AddChild( cloud);
    
    //Sprite* smallCloud = new Sprite( "cloud.png", 2);
    
    //smallCloud->SetPosition( 400, 300);
    
    //smallCloud->SetScale( 0.7);
    
    Sprite* background = new Sprite("skyline.png", 1);
    
    background->SetAnchorPoint(0.0f, 0.0f);
    
    background->SetPosition(0.0, 0.0f);
    
    Sprite* addBg = new Sprite( "skyline2.png", 2);
    
    addBg->SetPosition(1024, 0.0);
    
    AddChild( background, true);
    
    AddChild( addBg, true);
     
    //mBackground->AddChild(background2, true);
    
    //mBackground->AddChild( smallCloud);
    
    AddChild( mBackground);
    
    MemberFunction<DemoScene, void>* callback = new MemberFunction<DemoScene, void>( this, ( &DemoScene::buttonTouched));
    
    mNess->SetCallback( callback);
    
    mAnimation = new BounceTween( Meteor::Tween::Scale, Meteor::Tween::EaseNone, 3.0f, 1.0f, 0.25);
    
    mBackground->AddChild( mNess);
    
    label = new TextElement("Verdana.ttf", 0, 40);
    
    label->SetText("this is a text element");
    
    //mBackground->AddChild( label);
    
    label->SetPosition(0, 400);
    
    label->SetIndexZ( 5);
    
    //label->SetColor( 1.0, 0.0, 0.0, 1.0f);
    
    //AddChild( label);
    
    // mNess->SetAnchorPoint(1.0, 1.0);
    
    }
    catch ( const std::exception& exc)
    {
        return;
    }
}

// -----------------------------------------------------------------------------

DemoScene::~DemoScene()
{
}

// -----------------------------------------------------------------------------

void DemoScene::Update(const float deltaTime)
{
    Scene::Update( deltaTime);
    
    static float rot = 0.0f;
    
    //mNess->SetRotation(rot++);
    
   // mBackground->MoveBy( Vector2D( -3, 0));
}

// -----------------------------------------------------------------------------

void DemoScene::buttonTouched()
{
    std::cout << "button clicked" << std::endl;

    Meteor::GameController::Instance().RunScene( new MainMenu());
    
    /*
    static bool odd = true;

    if ( odd)
    {
        mBackground->ApplyTween( new BounceTween( Meteor::Tween::Alpha, Meteor::Tween::EaseOut, 1.0f, 0.0f, 1.5f));
        
        odd = false;    
    }
    else
    {
        //mNess->ApplyTween( new Tween( Meteor::Tween::Scale, Meteor::Tween::EaseNone, mNess->Scale(), 1.0f, 1.0f));
        
        odd = true;
    }
    */
}

// -----------------------------------------------------------------------------

void DemoScene::textureAdded()
{
    std::cout << "texture added" << std::endl;
}

// -----------------------------------------------------------------------------

void DemoScene::TouchBegan( const Meteor::Vector2D& position)
{
    
}

void DemoScene::TouchMoved( const Meteor::Vector2D& position)
{
    static float oldPosition = position.X();

    //mBackground->MoveBy( Vector2D( position.X()-oldPosition, 0));
    
    oldPosition = position.X();
}

void DemoScene::TouchEnded( const Meteor::Vector2D& position, double duration)
{
    Logger::DebugLog("scene clicked");
}

